package com.wx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wx.core.process.WxApi;
import com.wx.dao.WxMenuMapper;
import com.wx.entity.WxMenu;
import com.wx.entity.WxMenuExample;
import com.wx.service.SysWxTokenService;
import com.wx.service.WxMenuService;
import com.zyht.common.util.HttpUtils;

@Service("WxMenuService")
public class WxMenuImpl implements WxMenuService {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private WxMenuMapper wxMenuMapper;

	@Resource
	private SysWxTokenService sysWxTokenService;
	
	@Override
	public int insert(WxMenu wxMenu) {
		return wxMenuMapper.insert(wxMenu);
	}

	@Override
	public List<WxMenu> selectByExample(WxMenuExample example) {
		return wxMenuMapper.selectByExample(example);
	}
	@Override
	public WxMenu selectByPrimaryKey(Integer id) {
		return wxMenuMapper.selectByPrimaryKey(id);
	}
	@Override
	public int countByExample(WxMenuExample example) {
		return wxMenuMapper.countByExample(example);
	}
	@Override
	public int updateByExample(WxMenu record, WxMenuExample example) {
		return wxMenuMapper.updateByExample(record, example);
	}
	
	@Override
	public int updateByExampleSelectAble(WxMenu record, WxMenuExample example){
		return this.wxMenuMapper.updateByExampleSelective(record,example);
	};
	
	@Override
	public int updateByPrimaryKey(WxMenu record) {
		return wxMenuMapper.updateByPrimaryKey(record);
	}
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return wxMenuMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByPrimaryKey(WxMenuExample example) {
		return wxMenuMapper.deleteByExample(example);
	}

	@Override
	public boolean createWxMenus(int uid, String wxid, String appId, String appSecret, String menuJson) {
		String accessToken = sysWxTokenService.getAccessToken( wxid, appId, appSecret);
		if(logger.isDebugEnabled()){
			logger.debug("The AccessToken is "+ accessToken);
		}
		JSONObject reqJson = deleteWxMenus(accessToken);
		String json = "";
		if(null!=reqJson){
			json = reqJson.toString();
		}
		if(logger.isDebugEnabled()){
			logger.warn("System is processing delete menu ,the response is "+json);
		}
		if(null==reqJson||reqJson.getInteger("errcode")==0){
			String addWxMenu = WxApi.getMenuCreateUrl(accessToken);
			String result = HttpUtils.httpsPost(addWxMenu,menuJson);
			reqJson = JSONObject.parseObject(result);
			json = null==reqJson?"nil":reqJson.toString();
			logger.warn("To create menu,the response charquece is :" + json);
			if(null!=reqJson){
				return reqJson.getInteger("errcode")==0;
			}
		}
		return false;
	}
	
	@Override
	public JSONObject deleteWxMenus(String accessToken) {
		String delWxMenu = WxApi.getMenuDeleteUrl(accessToken);
		String response = HttpUtils.httpsGet(delWxMenu);
		return JSON.parseObject(response);
	}	
}